class ObjectTypesController < ApplicationController

  before_filter :get_museum

  def index
    @object_types = ObjectType.find_by_sql("SELECT object_types.id, object_types.name, count(entities.id) as 'count' FROM `categorisations` LEFT JOIN entities ON categorisations.entity_id = entities.id LEFT JOIN object_types ON categorisations.object_type_id = object_types.id WHERE entities.museum_id = 1 GROUP BY categorisations.object_type_id;")

    @top_object_types = ObjectType.find_by_sql("SELECT object_types.id, object_types.name, count(entities.id) as 'count' FROM `categorisations` LEFT JOIN entities ON categorisations.entity_id = entities.id LEFT JOIN object_types ON categorisations.object_type_id = object_types.id WHERE entities.museum_id = 1 GROUP BY categorisations.object_type_id ORDER BY count DESC LIMIT 0,8;")

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @object_types }
    end
  end

  # GET /object_types/1
  # GET /object_types/1.xml
  def show
    @object_type = ObjectType.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @object_type }
    end
  end

  # GET /object_types/new
  # GET /object_types/new.xml
  def new
    @object_type = ObjectType.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @object_type }
    end
  end

  # GET /object_types/1/edit
  def edit
    @object_type = ObjectType.find(params[:id])
  end

  # POST /object_types
  # POST /object_types.xml
  def create
    @object_type = ObjectType.new(params[:object_type])

    respond_to do |format|
      if @object_type.save
        format.html { redirect_to museum_object_type_path(@museum, @object_type) }
        format.xml  { render :xml => @object_type, :status => :created, :location => @object_type }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @object_type.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /object_types/1
  # PUT /object_types/1.xml
  def update
    @object_type = ObjectType.find(params[:id])

    respond_to do |format|
      if @object_type.update_attributes(params[:object_type])
        flash[:notice] = 'ObjectType was successfully updated.'
        format.html { redirect_to(@object_type) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @object_type.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /object_types/1
  # DELETE /object_types/1.xml
  def destroy
    @object_type = ObjectType.find(params[:id])
    @object_type.destroy

    respond_to do |format|
      format.html { redirect_to(object_types_url) }
      format.xml  { head :ok }
    end
  end
end
